package com.yjd.comm.base.control;

import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.MfqTokenModel;
import com.yjd.comm.base.service.IMfqTokenService;
import com.yjd.comm.smsverify.service.ISmsVerifyService;
import com.yjd.comm.system.model.SysParamModel;
import com.yjd.comm.system.util.SysParamUtil;
import com.yjd.comm.user.dto.UserSession;
import com.yjd.comm.user.enums.ClientChannelEnum;
import com.yjd.comm.user.util.UserUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.vo.CachedKeys;
import com.yjd.comm.vo.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <b>description</b>：移动端control的基类 <br>
 * <b>time</b>：2015-1-20下午4:32:03 <br>
 * <b>author</b>： ready likun_557@163.com
 */
public class MobileBaseControl extends BaseControl {

	@Override
	protected UserSession getUserSession() {
		return UserUtil.getUserModel(request, response,
				ClientChannelEnum.MOBILE);
	}/**
	 * 发送短信
	 * @param mpNumber
	 * @return boolean
	 * @throws Exception
	 */
	protected void sendMessage(String mpNumber) throws Exception {

		if (StringUtils.isBlank(mpNumber)) {
			FrameUtil.throwBaseException1("参数异常");
		}

		// 正则匹配
		SysParamModel sysParamModel = SysParamUtil
				.getSysParamModel(Constant.SysParamKeyEnum.REGISTER_REGEX);
		if (sysParamModel != null) {
			Matcher name = Pattern.compile(sysParamModel.getParam_value())
					.matcher(mpNumber);
			if (!name.matches()) {
				super.errorMsg("手机号格式不正确");
				return;
			}
		}
		// 发送短信
		int result = smsVerifyService.sendMessage(mpNumber);

		// 发送成功
		if (result == 0) {
			super.successData(FrameUtil.newHashMap("message",
					"短信已发送到您的" + UserUtil.disposeMobile(mpNumber) + "手机!"));
			return;
		}

		// 超过最大发送量
		if (result == -1000) {
			super.errorMsg("请不要连续发送验证码");
			return;
		}

		// 超过最大发送量
		if (result == -1001) {
			super.errorMsg("一天最多发送10次,请明天再来吧");
			return;
		}

		// 验证码不匹配
		if (result == -1002) {
			super.errorMsg("该手机获取验证码已达上限，请1小时后重试");
			return;
		}

		super.errorMsg("系统繁忙,请稍后再试");
	}


	
	/**
	 * 比较验证码
	 * 
	 * @param npNumber
	 * @param msgCode
	 * @return boolean
	 * @throws Exception
	 */
	protected boolean compareMessageCode(String npNumber, String msgCode)throws Exception {
		if (StringUtils.isBlank(npNumber) || StringUtils.isBlank(msgCode)
				|| !NumberUtils.isNumber(npNumber)) {
			FrameUtil.throwBaseException1("参数异常");
		}
		
		int msgCount = 1;
		// 验证码错误次数
		String keys = "DXYZ"+npNumber;
		 MfqTokenModel mfqTokenModel = mfqTokenService.getModelById(keys, DbWREnums.WRITE);
		if(mfqTokenModel!=null){
			msgCount = Integer.parseInt(mfqTokenModel.getToken_value());
		}

		int result = 0;
        result = smsVerifyService.compareMessageCode(npNumber, msgCode, msgCount, 3);
		
		//校验通过
		if(result == 0){
			return true;
		}
		
		//无效验证码
		if(result == -1000){
			return false;
		}
		
		//验证码达到最大错误次数
		if(result == -1001){
			ServletUtil.removeSession(request, response, CachedKeys.MSG_CODE_COUNT);
			return false;
		}
		
		//验证码不匹配
		if(result == -1002){
			ServletUtil.putSession(request, response, CachedKeys.MSG_CODE_COUNT, msgCount + 1);
			return false;
		}
		
		return false;
	}


	@Resource
	private ISmsVerifyService smsVerifyService;

	@Resource
	private IMfqTokenService mfqTokenService;

}
